package com.yisu.expressway.onedollar.activity;

import com.yisu.expressway.R;
import com.yisu.expressway.onedollar.widget.TitleView;
import com.yisu.expressway.utils.StringUtil;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import butterknife.Bind;

/**
 * Created by Kaven on 2016/12/9.
 */
public class DetailActivity extends BasicActivity {
    public static final int TYPE_HTML = 0;
    public static final int TYPE_URL = 1;
    private static final String EXTRA_KEY_URL = "extra_key_url";
    private static final String EXTRA_KEY_TITLE = "extra_key_title";
    private static final String EXTRA_KEY_TYPE = "extra_key_type";
    @Bind(R.id.title_bar)
    protected TitleView mTitleView;
    @Bind(R.id.webView)
    protected WebView mWebView;
    @Bind(R.id.pb_load)
    protected ProgressBar mLoadProgress;
    private String mData;
    private String mTitle;
    private int mDataType;

    public static void launch(Context context, String data, int type,String title) {
        Intent intent = new Intent(context, DetailActivity.class);
        intent.putExtra(EXTRA_KEY_URL, data);
        intent.putExtra(EXTRA_KEY_TYPE, type);
        intent.putExtra(EXTRA_KEY_TITLE, title);
        context.startActivity(intent);
    }

    @Override
    protected int getThemeId() {
        return R.style.OneDollarTheme;
    }

    @Override
    protected int getInflateLayoutId() {
        return R.layout.activity_onedollar_goods_detail;
    }

    @Override
    protected void initPresenter() {

    }

    @Override
    protected void releasePresenter() {

    }

    @Override
    protected void getIntentData(Intent intent) {
        String stringExtra = intent.getStringExtra(EXTRA_KEY_URL);
        if (TextUtils.isEmpty(stringExtra)) {
            finish();
        }
        mData = stringExtra;
        mDataType = intent.getIntExtra(EXTRA_KEY_TYPE, TYPE_HTML);
        mTitle = intent.getStringExtra(EXTRA_KEY_TITLE);
    }

    @Override
    protected void getSavedBundleData(Bundle bundle) {

    }

    @Override
    protected void initViews() {
        mTitleView.setTitle(mTitle);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        mWebView.setHorizontalScrollBarEnabled(false);
        mWebView.setHorizontalFadingEdgeEnabled(false);
        mWebView.setVerticalScrollBarEnabled(false);
        mWebView.setVerticalFadingEdgeEnabled(false);
        switch (mDataType) {
            case TYPE_HTML:
                mWebView.loadData(mData, "text/html", "utf-8");
                break;
            case TYPE_URL:
                mWebView.loadUrl(mData);
                break;
        }

        mWebView.setWebViewClient(new webViewClient());
        mWebView.setWebChromeClient(new jsWebChromeClient());
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            finish();
        }
    }

    @Override
    protected void bindEvents() {
        mTitleView.setOnTitleBarClickEvent(new TitleView.OnTitleBarClickEvent() {
            @Override
            public void onLeftClick(View view) {
                finish();
            }

            @Override
            public void onTitleClick(View view) {

            }

            @Override
            public void onRightClick(View view) {

            }
        });
    }

    @Override
    protected boolean isNeedLogin() {
        return false;
    }

    @Override
    protected void onLoginStateChange(boolean login) {

    }

    private class webViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (StringUtil.isAvailableWebUrl(url)) {
                if (mLoadProgress != null) {
                    mLoadProgress.setVisibility(View.VISIBLE);
                }
                view.loadUrl(url);
            }
            return false;
        }

        @SuppressLint("SetJavaScriptEnabled")
        @Override
        public void onPageFinished(WebView view, String url) {

        }

    }

    private class jsWebChromeClient extends WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int progress) {
            if (mLoadProgress != null) {
                mLoadProgress.setProgress(progress);
                if (progress == 100) {
                    mLoadProgress.setVisibility(View.GONE);
                }
            }
        }
    }
}
